package com.jjc.android.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import com.allen.library.SuperButton
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.view.CommonKt
import com.jjc.android.view.onClick
import es.voghdev.pdfviewpager.library.RemotePDFViewPager
import es.voghdev.pdfviewpager.library.VerticalPdfViewPager
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter
import es.voghdev.pdfviewpager.library.remote.DownloadFile
import es.voghdev.pdfviewpager.library.util.FileUtil

class PdfViewActivity : BaseActivity() {

    companion object{
        fun start(context: Context,url:String,title:String){
            val intent = Intent(context,PdfViewActivity::class.java)
            intent.putExtra(Constants.BUNDLE_INFO,url)
            intent.putExtra("title",title)
            context.startActivity(intent)
        }
    }

    override val layoutId: Int = R.layout.activity_pdf_view
    private val downloadBtn: SuperButton by lazy { findViewById<SuperButton>(R.id.downloadBtn) }
    private val pdfLayout: FrameLayout by lazy { findViewById(R.id.pdfLayout) }
    private var remotePDFViewPager:VerticalPdfViewPager? = null
    private var adapter:PDFPagerAdapter? = null
    private var pdfUrl:String? = null
    private var pdfTitle:String? = null
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(pdfTitle?:getString(R.string.operate_book_txt))
        showLoadingView(pdfLayout)
        if(!pdfUrl.isNullOrEmpty()){
            remotePDFViewPager = VerticalPdfViewPager(this)
            downloadBtn.onClick {
                if(localFilePath.isNullOrEmpty()){
                    CommonKt.downloadFile(this,pdfUrl,true,{
                        ToastUtils.show("${getString(R.string.down_fail_txt)}:$it")
                    }){
                        pdfUrl = it.absolutePath
                        ToastUtils.show("${getString(R.string.down_success_txt)}:${it.absolutePath}")
                    }
                }else{
                    ToastUtils.show("${getString(R.string.down_success_txt)}:$localFilePath")
                }
            }
            CommonKt.downloadFile(this,pdfUrl,false,{
                onFailure(java.lang.Exception(it))
            }){
                localFilePath = it.absolutePath
                onSuccess(localFilePath)
            }
            downloadBtn.isEnabled = true
        }else{
            showEmptyView()
        }

    }
    private var localFilePath:String? = null
    private fun onSuccess(destinationPath: String?) {
        adapter = PDFPagerAdapter(this, destinationPath)
        remotePDFViewPager?.adapter = adapter
        pdfLayout.removeAllViews()
        remotePDFViewPager?.let {
            pdfLayout.addView(remotePDFViewPager,
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
            )
        }
        hideLoadingView()
    }

    private fun onFailure(e: Exception?) {
        showErrorView(e?.message)
    }


    override fun onDestroy() {
        super.onDestroy()
        adapter?.close()
    }

    override fun setupArguments(args: Bundle?) {
        pdfUrl = args?.getString(Constants.BUNDLE_INFO)
        pdfTitle =  args?.getString("title")
    }
}